{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Appendix.ipynb",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "xp3o3U4RNcR4"
      },
      "source": [
        "# **String Manipulation**"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "9nIJgaJq1ng-"
      },
      "source": [
        "\n",
        "\n",
        "1.   **Creating a string**\n",
        "\n",
        ">Strings in Python can be created using single quotes or double quotes or even triple quotes.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "mzYAsY-r1417",
        "outputId": "0c011722-b3c7-49d6-d988-4caa6ea639da",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 68
        }
      },
      "source": [
        "# Python Program for creating string\n",
        "   \n",
        "String1 = 'Creating a String  with single Quotes.'\n",
        "String2 = \"Creating a String  with double Quotes.\"\n",
        "String3 = '''Creating a String  with triple Quotes.'''\n",
        "print(String1) \n",
        "print(String2) \n",
        "print(String3) \n",
        "  \n"
      ],
      "execution_count": 1,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Creating a String  with single Quotes.\n",
            "Creating a String  with double Quotes.\n",
            "Creating a String  with triple Quotes.\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "F8fMkd6Y5T-Y"
      },
      "source": [
        "\n",
        "\n",
        "2.   **Accessing characters in Python**\n",
        "\n",
        "\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "FhG3IBvr3qqY",
        "outputId": "ec44b8e1-d860-4893-97ec-22595c367a9a",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 119
        }
      },
      "source": [
        "# Python Program to Access characters of String \n",
        "# characters of String \n",
        "  \n",
        "String = \"Exploratory Data Analysis\"\n",
        "\n",
        "# Printing First character \n",
        "print(\"\\nFirst character of String is: \") \n",
        "print(String[0]) \n",
        "  \n",
        "# Printing Last character \n",
        "print(\"\\nLast character of String is: \") \n",
        "print(String[-1]) "
      ],
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "\n",
            "First character of String is: \n",
            "E\n",
            "\n",
            "Last character of String is: \n",
            "s\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "5fbvh8eW6orE"
      },
      "source": [
        "3.   **String Slicing**\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "zAIKBTKr65pH",
        "outputId": "4c563883-b283-41d8-f5b5-f5f93401ee56",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 119
        }
      },
      "source": [
        "# Python Program to demonstrate String slicing \n",
        "  \n",
        "# Creating a String \n",
        "String = \"Exploratory Data Analysis\"\n",
        "  \n",
        "# Printing 3rd to 12th character \n",
        "print(\"\\nSlicing characters from 3-12: \") \n",
        "print(String[3:12]) \n",
        "  \n",
        "# Printing characters between 3rd and 2nd last character \n",
        "print(\"\\nSlicing characters between \" +\n",
        "    \"3rd and 2nd last character: \") \n",
        "print(String[3:-2]) "
      ],
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "\n",
            "Slicing characters from 3-12: \n",
            "loratory \n",
            "\n",
            "Slicing characters between 3rd and 2nd last character: \n",
            "loratory Data Analys\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "2Hglxt5z7Tmn"
      },
      "source": [
        "4.   **Deleting/Updating from a String**\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "zfmYCpKB701M",
        "outputId": "b31906a9-999d-4a57-8c8f-453a60f51bb6",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 232
        }
      },
      "source": [
        "# Updation of a character\n",
        "\n",
        "String = \"Exploratory Data Analysis\"\n",
        "\n",
        "String[2] = 'p'\n",
        "print(\"\\nUpdating character at 2nd Index: \") \n",
        "print(String) "
      ],
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "error",
          "ename": "TypeError",
          "evalue": "ignored",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-4-81ab93ce3a35>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0mString\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"Exploratory Data Analysis\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mString\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'p'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"\\nUpdating character at 2nd Index: \"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      6\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mString\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "OUFBpRrm8WXY",
        "outputId": "642f9aff-a633-405a-c1ed-1bf3cd657f6b",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 119
        }
      },
      "source": [
        "# Update entire String \n",
        "  \n",
        "String1 = \"Exploratory Data Analysis\"\n",
        "print(\"\\nInitial String: \")\n",
        "print(String1)  \n",
        "\n",
        "String2 = \"Welcome to the world of Data Science.\"\n",
        "print(\"\\nUpdated String: \") \n",
        "print(String2) "
      ],
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "\n",
            "Initial String: \n",
            "Exploratory Data Analysis\n",
            "\n",
            "Updated String: \n",
            "Welcome to the world of Data Science.\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "aVO_mA_z83lJ",
        "outputId": "725c38e7-6080-4d81-970b-a130a2bcab60",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 215
        }
      },
      "source": [
        "# Delete characters from a String \n",
        "  \n",
        "String = \"Exploratory Data Analysis\"\n",
        "  \n",
        "del String[2]  \n",
        "print(String) "
      ],
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "error",
          "ename": "TypeError",
          "evalue": "ignored",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-6-061dfc950dd5>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0mString\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"Exploratory Data Analysis\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0;32mdel\u001b[0m \u001b[0mString\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mString\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;31mTypeError\u001b[0m: 'str' object doesn't support item deletion"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "A7pywlHA9eL6",
        "outputId": "831fb7c8-79e6-408e-c5dc-7aed63186f94",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 198
        }
      },
      "source": [
        "# Delete characters from a String \n",
        "  \n",
        "String = \"Exploratory Data Analysis\"\n",
        "  \n",
        "del String  \n",
        "print(String) "
      ],
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "error",
          "ename": "NameError",
          "evalue": "ignored",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-7-3b421a247810>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0;32mdel\u001b[0m \u001b[0mString\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mString\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
            "\u001b[0;31mNameError\u001b[0m: name 'String' is not defined"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "lXMbROKG_VC4"
      },
      "source": [
        "5.   **Escape Sequencing in Python**\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "TdYzP5mWAVMb",
        "outputId": "a57b619d-9372-4f98-d059-d2989091ec4a",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 323
        }
      },
      "source": [
        "# Python Program for Escape Sequencing  of String \n",
        "  \n",
        "String = '''I'm a \"Data Scientist\"'''\n",
        "\n",
        "# Initial String \n",
        "print(\"Initial String with use of Triple Quotes: \") \n",
        "print(String) \n",
        "\n",
        "# Escaping Single Quote  \n",
        "String = 'I\\'m a \"Data Scientist\"'\n",
        "print(\"\\nEscaping Single Quote: \") \n",
        "print(String) \n",
        "  \n",
        "# Escaping Doule Quotes \n",
        "String = \"I'm a \\\"Data Scientist\\\"\"\n",
        "print(\"\\nEscaping Double Quotes: \") \n",
        "print(String) \n",
        "  \n",
        "# Printing Paths with the use of Escape Sequences \n",
        "String = \"C:\\\\Python\\\\DataScience\\\\\"\n",
        "print(\"\\nEscaping Backslashes: \") \n",
        "print(String) \n",
        "\n",
        "# To ignore the escape sequences in a String,r or R \n",
        "# is used, this implies that the string is a raw string\n",
        "# and escape sequences inside it are to be ignored.\n",
        "\n",
        "# Printing statements in diffrent lines \n",
        "Statements = \"Lets talk about EDA.\\n EDA is an acronym for Exploratory Data Analysis\"\n",
        "print(\"\\nPrinting with the use of Escape Sequences: \") \n",
        "print(Statements) \n",
        "  \n",
        "# Using raw String to  \n",
        "# ignore Escape Sequences \n",
        "Statements = r\"Lets talk about EDA.\\n EDA is an acronym for Exploratory Data Analysis\"\n",
        "print(\"\\nPrinting Raw String : \") \n",
        "print(Statements) \n",
        "\n"
      ],
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Initial String with use of Triple Quotes: \n",
            "I'm a \"Data Scientist\"\n",
            "\n",
            "Escaping Single Quote: \n",
            "I'm a \"Data Scientist\"\n",
            "\n",
            "Escaping Double Quotes: \n",
            "I'm a \"Data Scientist\"\n",
            "\n",
            "Escaping Backslashes: \n",
            "C:\\Python\\DataScience\\\n",
            "\n",
            "Printing with the use of Escape Sequences: \n",
            "Lets talk about EDA.\n",
            " EDA is an acronym for Exploratory Data Analysis\n",
            "\n",
            "Printing Raw String : \n",
            "Lets talk about EDA.\\n EDA is an acronym for Exploratory Data Analysis\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "idAS9XANHANO"
      },
      "source": [
        "6.   **Formatting of Strings**\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "crHv47XQMOzZ",
        "outputId": "32953e00-8d29-4d53-e982-58c2174215ed",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 153
        }
      },
      "source": [
        "# Python Program for Formatting of Strings \n",
        "  \n",
        "# Default order \n",
        "String1 = \"{} {} {}\".format('Exploratory ', 'Data ', 'Analysis') \n",
        "print(\"Print String in default order: \") \n",
        "print(String1) \n",
        "  \n",
        "# Positional Formatting \n",
        "String1 = \"{1} {0} {2}\".format('Exploratory', 'Data', 'Analysis') \n",
        "print(\"\\nPrint String in Positional order: \") \n",
        "print(String1) \n",
        "  \n",
        "# Keyword Formatting \n",
        "String1 = \"{l} {f} {g}\".format(g = 'Exploratory', f = 'Data', l = 'Analysis') \n",
        "print(\"\\nPrint String in order of Keywords: \") \n",
        "print(String1) "
      ],
      "execution_count": 10,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Print String in default order: \n",
            "Exploratory  Data  Analysis\n",
            "\n",
            "Print String in Positional order: \n",
            "Data Exploratory Analysis\n",
            "\n",
            "Print String in order of Keywords: \n",
            "Analysis Data Exploratory\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "TCxx1p4fNH41"
      },
      "source": [
        "## Working with text dataset"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "AzBEMVdA4YPo"
      },
      "source": [
        "For string formatting taking a little messier dataset would bee better. Kaggle launched a data exploration competition, giving users access to a database of comments made on Reddit.com during May 2015. I extracted the comments from the team's fan subreddit from the database of one of the popular basketball team Minnesota Timberwolves. You can get the data file (comments.csv)\n",
        "[here](https://www.kaggle.io/svf/41614/79c92bacce098553297dbeec6c7c2196/comments.csv).\n",
        "\n",
        "Let's start by loading the data and checking its structure and a few of the comments:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "YanCA-NqRPyp",
        "colab": {}
      },
      "source": [
        "import numpy as np\n",
        "import pandas as pd\n",
        "import os"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "ZGkbSilhRVLD",
        "outputId": "abe182f0-abd3-4d27-d840-3ccd44f5f5b6",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 187
        }
      },
      "source": [
        "\n",
        "text = pd.read_csv(\"comments.csv\")\n",
        "\n",
        "text = text[\"body\"]     # Convert from df to series\n",
        "\n",
        "print (text.shape)\n",
        "\n",
        "print( text.head(8))"
      ],
      "execution_count": 13,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "(4166,)\n",
            "0    Strongly encouraging sign for us.  The T-Wolve...\n",
            "1    [My reaction.](http://4.bp.blogspot.com/-3ySob...\n",
            "2                     http://imgur.com/gallery/Zch2AWw\n",
            "3    Wolves have more talent than they ever had rig...\n",
            "4    Nah. Wigg is on the level of KG but where's ou...\n",
            "5           2004 was a pretty damn talented team dude.\n",
            "6                                                  :')\n",
            "7                                              *swoon*\n",
            "Name: body, dtype: object\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "qMbKivHU_vRj",
        "colab_type": "text"
      },
      "source": [
        "# Pandas String Functions"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "UhnCSFu-_vRn",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# str.lower() to convert the first comment to lowercase \n",
        "text[0].lower() "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "x74Pr-Zh_vRx",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Convert all comment to lowercase str.lower()\n",
        "text.str.lower().head(8)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "AfSzbArq_vR4",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# str.upper() to convert the first comment to uppercase \n",
        "text[0].upper() "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "QNCKUbNT_vSA",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# str.len() to Get the length of all comments\n",
        "text.str.len().head(8)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "5sZ-6ZBo_vSH",
        "colab_type": "text"
      },
      "source": [
        "The string splitting and stripping functions also have pandas equivalents:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "0gHZGJRu_vSK",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Split comments on spaces\n",
        "text.str.split(\" \").head(8)  "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "RgNeW1l6_vSV",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Strip leading and trailing brackets\n",
        "text.str.strip(\"[]\").head(8) "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "oeoWWk5c_vSl",
        "colab_type": "text"
      },
      "source": [
        "Combine all the strings in a series together into a single string with series.str.cat():"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Hhvx7ECf_vSs",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        " # Check the first 500 characters\n",
        "text.str.cat()[0:500]  "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "YS1oCKQE_vS1",
        "colab_type": "text"
      },
      "source": [
        "You can slice each string in a series and return the result in an elementwise fasion with series.str.slice():"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "EweTF5I5_vS3",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Slice the first 10 characters\n",
        "text.str.slice(0, 10).head(8)  "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "hRLp3XkJ_vS9",
        "colab_type": "text"
      },
      "source": [
        "You can also use indexing after series.str to take slices:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7JTJAPC2_vTA",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Slice the first 10 characters\n",
        "text.str[0:10].head(8)  "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "WaCtukDs_vTI",
        "colab_type": "text"
      },
      "source": [
        "Replace a slice with a new substring using str.slice_replace():"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "h5DwdIii_vTK",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "text.str.slice_replace(5, 10, \" Love you Wolves \" ).head(8)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "rOzB0R16_vTV",
        "colab_type": "text"
      },
      "source": [
        "Replace the occurences of a given substring with a different substring using str.replace():"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "5p1NuQAa_vTY",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "text.str.replace(\"Wolves\", \"Fox\").head(8)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "c2bqO7vo_vTf",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        " # Get first 10 comments about Andrew Wiggins\n",
        "selected_comments = text.str.lower().str.contains(\"wigg|drew\")\n",
        "\n",
        "text[selected_comments].head(10)    "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "enR58mao_vTm",
        "colab_type": "text"
      },
      "source": [
        "Just for information lets calculate the ratio of comments that mention Andrew Wiggins:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "qJNuU60I_vTp",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "len(text[selected_comments])/len(text)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "vEi9rl1P_vT4",
        "colab_type": "text"
      },
      "source": [
        "# Regular Expressions"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "9izgu6tc_vT_",
        "colab_type": "text"
      },
      "source": [
        "\".\" - The period is a metacharacter that matches any character other than a newline:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Z6vRN0rB_vUB",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Match any substring ending in ill\n",
        "my_words = pd.Series([\"abaa\",\"cabb\",\"Abaa\",\"sabb\",\"dcbb\"])\n",
        " \n",
        "my_words.str.contains(\".abb\") "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "v2lI3trg_vUI",
        "colab_type": "text"
      },
      "source": [
        "\"[ ]\" - Square brackets specify a set of characters to match:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "cfIh4mZL_vUK",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Matches T or t followed by \"ill\"\n",
        "my_words.str.contains(\"[Aa]abb\")"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "daE9JBUW_vUS",
        "colab_type": "text"
      },
      "source": [
        "\"^\" - outside of square brackets, the caret symbol searches for matches at the beginning of a string:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "scrolled": true,
        "id": "jSASp9r3_vUU",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Matches He or he at the start of a string\n",
        "Sentence_series= pd.Series([\"Where did he go\", \"He went to the shop\", \"he is good\"])\n",
        "\n",
        "Sentence_series.str.contains(\"^(He|he)\")  "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "sdsD0x6t_vUe",
        "colab_type": "text"
      },
      "source": [
        "\"$\" - searches for matches at the end of a string:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "_39lWIoA_vUg",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Matches go at the end of a string\n",
        "Sentence_series.str.contains(\"(go)$\") "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "HTGkEeg7_vUq",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Match 0 or more a's, a single b, then 1 or characters\n",
        "\n",
        "Word_series1 = pd.Series([\"abnormal\",\"bb\",\"a\",\"abbcc\",\"aba\"])\n",
        "\n",
        "Word_series1.str.contains(\"a*b.+\") "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "G9lj7ZQu_vU2",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Match 2 a's then 2 or more b's\n",
        "\n",
        "Word_series2 = pd.Series([\"abbaab\",\"abbb\",\"aabcbcb\",\"aabb\"])\n",
        "\n",
        "Word_series2.str.contains(\"a{2}b{1,}\") "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "h2kmmzHC_vVC",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Match a single period and then a space\n",
        "\n",
        "Word_series3 = pd.Series([\"Mr. SK\",\"Dr. Deepak\",\"Miss\\Mrs Gaire.\"])\n",
        "\n",
        "Word_series3.str.contains(\"\\. \")"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "_54oOoPM_vVN",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Match strings containing a backslash\n",
        "Word_series3.str.contains(r\"\\\\\")"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Q_6qzILz_vVZ",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "text.str.count(r\"[Ww]olves\").head(8)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "K0Z4_Lvk_vVg",
        "colab_type": "text"
      },
      "source": [
        "Use series.str.findall() to get each matched substring and return the result as a list:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "SJ5clgBi_vVq",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "text.str.findall(r\"[Ww]olves\").head(8)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "VRlAmra9_vV0",
        "colab_type": "text"
      },
      "source": [
        "# Getting Posts with Web Links"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "utG0OrzS_vV4",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "web_links = text.str.contains(r\"https?:\")\n",
        "\n",
        "posts_with_links = text[web_links]\n",
        "\n",
        "print( len(posts_with_links))\n",
        "\n",
        "posts_with_links.head(5)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "HlxJQeL6_vWC",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "only_links = posts_with_links.str.findall(r\"https?:[^ \\n\\)]+\")\n",
        "\n",
        "only_links.head(10)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "-Hm8tHeb_vWH",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        ""
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}